/**
 * UserInterface.java (Login)
 */
package edu.gatech.coc.betameisters.userinterfaces;

// $codepro.audit.disable
/**
 * This is the UserInterface class. It doesn't do much now except for check for logins
 * @author Henry
 * @version 1.0
 */
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import edu.gatech.coc.betameisters.people.Person;
import edu.gatech.coc.betameisters.utilities.Database;

/**
 * The Class UserInterface.
 */
public class UserInterface extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6145879112401157947L;

	/** field for username. */
	private JTextField userField;

	/** field for password. */
	private JTextField passField;

	/** fields for the buttons. */
	private JButton loginButton, exitButton;

	/** number of times user has tried to log in. */
	private int loginAttempts;

	/** number of times user may try to log in. */
	private final int MAX_ATTEMPTS = 3;

	/**
	 * necessary to be an instance variable so the actionlistener can change it.
	 */
	private Person returnValue;

	/** field for error messages. */
	private JLabel alert;

	/** action listener needs to be able to see this. */
	private boolean done;

	/**
	 * Default constructor. login() will be automatically called.
	 */
	public UserInterface() {
		login();
	}

	/**
	 * This is just another constructor for when you don't want login() to be
	 * automatically called
	 * 
	 * @param autoRun
	 *            when true, login() will be automatically called.
	 */
	public UserInterface(boolean autoRun) {
		if (autoRun) {
			login();
		}

	}

	/**
	 * @wbp.parser.entryPoint This is the login method. It brings up a frame and
	 *                        lets the user try to login three times.
	 * @return String the username of the user that logged in. returns null if
	 *         locked out.
	 */
	public Person login() {

		setPreferredSize(new Dimension(600, 300));

		done = false;
		returnValue = null;
		loginAttempts = 0;

		setLayout(null);

		JLabel lblUsername = new JLabel("UserName: ");
		lblUsername.setBounds(141, 107, 84, 14);
		add(lblUsername);

		userField = new JTextField();
		userField.setBounds(217, 104, 86, 20);
		add(userField);
		userField.setColumns(10);
		userField.addActionListener(new LoginActionListener());

		JLabel lblPassword = new JLabel("Password:");
		lblPassword.setBounds(141, 132, 64, 14);
		add(lblPassword);

		passField = new JTextField();
		passField.setBounds(217, 129, 86, 20);
		add(passField);
		passField.setColumns(10);
		passField.addActionListener(new LoginActionListener());

		alert = new JLabel("");
		alert.setBounds(188, 224, 46, 14);
		add(alert);

		loginButton = new JButton("Login");
		loginButton.setBounds(126, 173, 89, 23);
		add(loginButton);
		loginButton.addActionListener(new LoginActionListener());

		exitButton = new JButton("Exit");
		exitButton.setBounds(217, 173, 89, 23);
		add(exitButton);
		exitButton.addActionListener(new LoginActionListener());

		System.out.println("About to return:" + returnValue);
		return returnValue;
	}

	/**
	 * The action listener for the buttons to control the login.
	 * 
	 * @author Henry
	 */
	private class LoginActionListener implements ActionListener {
		/**
		 * handles the button logic for logins
		 * 
		 * @param e
		 *            the ActionEvent to determine source
		 */
		@SuppressWarnings("unused")
		public void actionPerformed(ActionEvent e) {
			Database database = new Database();
			if (e.getSource() == loginButton) {
				loginAttempts += 1;
				Person toLoad = Database.loadPatient(userField.getText());
				if (toLoad != null
						&& toLoad.getUserPass().equals(passField.getText())) {
					done = true;
					returnValue = toLoad;
					alert.setText("Welcome, " + userField.getText());
				}
				toLoad = Database.loadNurse(userField.getText());
				if (toLoad != null
						&& toLoad.getUserPass().equals(passField.getText())) {
					done = true;
					returnValue = toLoad;
					alert.setText("Welcome, " + userField.getText());
				}
				toLoad = Database.loadDoctor(userField.getText());
				if (toLoad != null
						&& toLoad.getUserPass().equals(passField.getText())) {
					done = true;
					returnValue = toLoad;
					alert.setText("Welcome, " + userField.getText());
				}

				if (done == false)// it must have been the wrong username if
									// we're not done
				{
					alert.setText("Wrong Username");
				}

			} else if (e.getSource() == exitButton) {
				done = true;
				returnValue = null;
			}
			if (loginAttempts >= MAX_ATTEMPTS) {
				alert.setText("Too many login attempts");
				done = true;
				returnValue = null;
			}
		}
	}

	public String toString() {
		return "UserInterface Login Screen";
	}
/*
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		UserInterface theInterface = new UserInterface(false);
		frame.add(theInterface);
		theInterface.login();
	}*/
}
